<?php

require('./php_procedures.php');
if (!isset($_POST['req'])) {
    die("No request provided.");

//---------------------LLENANDO PRIVILEGIOS USUARIO ---------------------------------------------
} else if (filter_input(INPUT_POST, 'req') == "RMAN") {

    $Completo = filter_input(INPUT_POST, 'Completo_');
    $Parcial = filter_input(INPUT_POST, 'Parcial_');
    $Incremental = filter_input(INPUT_POST, 'Incremental_');

    $str = "";

//ACTIVO ARCHIVE
    $ArchiveDesactivado = procedure_call("ARCMODE");
    $ArchiveDesactivado = implode($ArchiveDesactivado[0]);

    if ($ArchiveDesactivado == "NOARCHIVELOG") {
        $str .= "shutdown immediate;\n";
        $str .= "startup mount;\n";
        $str .= "ALTER DATABASE ARCHIVELOG;\n";
        $str .= "ALTER DATABASE OPEN;\n";
        $str .= "ALTER SYSTEM ARCHIVE LOG START;\n";
    }
//CREO TABLESPACE
    $str .= "DROP TABLESPACE RMAN_MANUAL INCLUDING CONTENTS AND DATAFILES;\n";
    $str .= "CREATE TABLESPACE RMAN_MANUAL DATAFILE 'C:\RMAN_MANUAL.DBF' SIZE 20M AUTOEXTEND ON;\n";

//CREO USUARIO
    $str .= "DROP USER RMANLAB CASCADE;\n";
    $str.= "CREATE USER RMANLAB IDENTIFIED BY RMANLAB DEFAULT TABLESPACE RMAN_MANUAL QUOTA UNLIMITED ON RMAN_MANUAL;\n";

//AGREGO PERMISOS AL USUARIO
    $str .= "GRANT CONNECT TO RMANLAB;\n";
    $str .= "GRANT RECOVERY_CATALOG_OWNER TO RMANLAB;\n";


//NOS CONECTAMOS A LA HERRAMIENTA RMAN 
    $str.=" $ RMAN TARGET / CATALOG RMANLAB/RMANLAB@orcl;\n";
//--PASS: RMANLAB
//CREAMOS CATALOGO DE RECUPERACION
    $str .= "CREATE CATALOG;\n";

//SINCRONIZAMOS CATALOGO DE RECUPERACION
    $str .= "REGISTER DATABASE;\n";

//RESPALDO PARCIAL
    //ECHO("El valor de parcial es :    ".$Parcial." \n  Parcial == true\n ");
    if ($Parcial == "verdad") {

        //RESPALDO TABLESPACE
        $str .= " BACKUP  TABLESPACE USERS;\n";
        // RESPALDO SPFILE
        $str .= "BACKUP SPFILE;\n";
        //RESPLAFO ARCHIVE
        $str .= "BACKUP AS COMPRESSED BACKUPSET ARCHIVELOG ALL TAG prod_ARCH_1124_2321;\n";

        //RESPLADO CONTROL FILES
        $str .= "BACKUP CURRENT CONTROLFILE;\n";
    }


//respaldo completo
    if ($Completo == "verdad") {

        $str .= "BACKUP DATABASE PLUS ARCHIVELOG;\n";
    }
//respaldo incremental

    if ($Incremental == "verdad") {
        //PARA DETERMINAR CUAL RESPALDAR
        $enaBlock = procedure_call("INCMODE");
        $enaBlock = implode($enaBlock[0]);

        if ($enaBlock == "DISABLED") {
            $str .="  ALTER DATABASE ENABLE BLOCK CHANGE TRACKING ;\n";
            // ESTO ES PARA MEJORAR EL BACKUP INCREMENTARL Y RMAN NO DEBA LEER TODOS LO BLOESQUE  
        }
        $str .= "BACKUP INCREMENTAL LEVEL 1 DATABASE;\n";
    }
} else if (filter_input(INPUT_POST, 'req') == "RMANParcial") {

    $TBS1 = json_decode(filter_input(INPUT_POST, 'tbs_'));
    $TBS2 = json_decode(filter_input(INPUT_POST, 'tbs2_'));
    $SPFILE = json_decode(filter_input(INPUT_POST, 'spfile_'));
    $CONTROLS = json_decode(filter_input(INPUT_POST, 'controls_'));
    $ARCHIVES = json_decode(filter_input(INPUT_POST, 'archives_'));
    $LOGS = json_decode(filter_input(INPUT_POST, 'logs_'));
    $path1 = filter_input(INPUT_POST, 'path_');

    $str = "";
    $str .= "--El path es:      " . $path1 . "\n";
//ACTIVO ARCHIVE
    $ArchiveDesactivado = procedure_call("ARCMODE");
    $ArchiveDesactivado = implode($ArchiveDesactivado[0]);

    if ($ArchiveDesactivado == "NOARCHIVELOG") {
        $str .= "shutdown immediate;\n";
        $str .= "startup mount;\n";
        $str .= "ALTER DATABASE ARCHIVELOG;\n";
        $str .= "ALTER DATABASE OPEN;\n";
        $str .= "ALTER SYSTEM ARCHIVE LOG START;\n";
    }
    $str .= "DROP TABLESPACE RMAN_MANUAL INCLUDING CONTENTS AND DATAFILES;\n";
    $str .= "CREATE TABLESPACE RMAN_MANUAL DATAFILE 'C:\RMAN_MANUAL.DBF' SIZE 20M AUTOEXTEND ON;\n";

//CREO USUARIO
    $str .= "DROP USER RMANLAB CASCADE;\n";
    $str.= "CREATE USER RMANLAB IDENTIFIED BY RMANLAB DEFAULT TABLESPACE RMAN_MANUAL QUOTA UNLIMITED ON RMAN_MANUAL;\n";
//AGREGO PERMISOS AL USUARIO
    $str .= "GRANT CONNECT TO RMANLAB;\n";
    $str .= "GRANT RECOVERY_CATALOG_OWNER TO RMANLAB;\n";


//NOS CONECTAMOS A LA HERRAMIENTA RMAN 
    $str.=" $ RMAN TARGET / CATALOG RMANLAB/RMANLAB@orcl;\n";
//--PASS: RMANLAB
//CREAMOS CATALOGO DE RECUPERACION
    $str .= "CREATE CATALOG;\n";

//SINCRONIZAMOS CATALOGO DE RECUPERACION
    $str .= "REGISTER DATABASE;\n";
    //RESPALDO TABLESPACE
    foreach ($TBS1 as $tbs) {
        $str .= "BACKUP  TABLESPACE " . $tbs . ";\n";
    }
    // RESPALDO SPFILE
    if (sizeof($SPFILE) > 0) {
        $str .= "BACKUP SPFILE;\n";
    }
    //RESPALDO ARCHIVE
    if (sizeof($ARCHIVES) > 0) {
        //
        $str .= "BACKUP ARCHIVELOG ALL;;\n";
    }
    //RESPLADO CONTROL FILES
    $i = 0;
    foreach ($CONTROLS as $c) {

        if (strpos($c, '\\\\') == FALSE) {
            
            str_replace("\\", "/", $path1);
            //$str .= "Respalda control";
            $str .= "BACKUP AS COPY CURRENT CONTROLFILE FORMAT 'C:/control".$i.".ctl'; \n";//control01.ctl
        } else {
            
            str_replace("\\", "/", $path1);
            
            $str .= "BACKUP AS COPY CURRENT CONTROLFILE FORMAT 'C:/control".$i.".ctl';\n";
        }
         $i = $i+1;
    }
//        //RESPALDO LOGS
//        foreach ($LOGS as $log)
//        {
//                $str .= "BACKUP ".$log." \n";
//        }
//        
}

//CREO EL ARCHIVO SQL
$Run_ = fopen("run.sql", "w+") or die("No abrió run.sql");
fwrite($Run_, $str); //TODO LO QUE AGREGUE LO ESCRIBO  EN EL SQL
//creo .bat 
$bat_ = fopen("cmd.bat", "w+") or die(json_encode("No .bat"));
fwrite($bat_, "@ECHO OFF\nexit|sqlplus -s sys as sysdba/root@orcl @run_rman.sql");

//EJECUTO EL .BAT 
 xec("cmd.bat");//ejecuto todo
